import {
  validatenull
} from './validate'
import store from '../store'

export const initMenu = (router, menu) => {
  if (menu.length === 0) {
    return
  }
  const menus = formatRoutes(menu)
  const unfound = {
    path: '*',
    redirect: '/404',
    hidden: true
  }
  menus.push(unfound)
  router.addRoutes(menus)
  store.commit('ADD_ROUTERS', menus)
}

export const formatRoutes = (aMenu) => {
  const aRouter = []
  aMenu.forEach(oMenu => {
    const {
      path,
      component,
      name,
      icon,
      url,
      children
    } = oMenu
    if (!validatenull(component)) {
      const oRouter = {
        path: path,
        component(resolve) {
          let componentPath = ''
          if (component === 'Layout') {
            require(['../views/layout/Layout'], resolve)
            return
          } else {
            componentPath = component
          }
          require([`../${componentPath}.vue`], resolve)
        },
        name: name,
        meta: {
          icon: icon,
          title: name,
          url: url
        },

        icon: icon,
        children: validatenull(children) ? [] : formatRoutes(children)
      }
      aRouter.push(oRouter)
    }
  })
  return aRouter
}

export function dateFormatter(str, type, lastDay, afterMonth) {
  var d = new Date(str)
  if (lastDay) {
    d = new Date(d.getFullYear(), d.getMonth() + 1, 0)
  }
  if (afterMonth) {
    d = new Date(d.getFullYear(), d.getMonth(), 0)
  }
  if(lastDay == "oneMonth"){
    //取当前年的1月，如果是今年的1月，取上一年的1月。如果不是，即去当年的一月
    if(d.getMonth() == 0){
      d = new Date(d.getFullYear() - 1, 1, 0)
    }else{
      d = new Date(d.getFullYear(), 1, 0)
    }
  }
  if(lastDay == "beforeThisMonth"){
    //取当前日期的上一月，如果当前日期是1月，则取去年12月。如果不是，即取上1月
    if(d.getMonth() == 0){
      d = new Date(d.getFullYear() - 1, 11, 0)
    }else{
      d = new Date(d.getFullYear(), d.getMonth(), 0)
    }
  }

  if(lastDay == "lastOneMonth"){
    //根据当前月，获取去年的1月
    if(d.getMonth() == 0){
      d = new Date(d.getFullYear() - 2, 1, 0)
    }else{
      d = new Date(d.getFullYear() - 1, 1, 0)
    }
  }

  if(lastDay == "lastThisMonth"){
    //根据当前月，获取去年的上一月
    if(d.getMonth() == 0){
      d = new Date(d.getFullYear() - 2, 11, 0)
    }else{
      d = new Date(d.getFullYear() - 1, d.getMonth(), 0)
    }
  }



  var year = d.getFullYear()
  var month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1)
  var day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate()
  var hour = d.getHours() < 10 ? '0' + d.getHours() : d.getHours()
  var minute = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes()
  var second = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds()
  // var SS = d.getMilliseconds()
  if (type === 0) {
    return [year, month, day].join('-') + ' ' + [hour, minute, second].join(':')
  } else if (type === 1) {
    return [year, month, day].join('-')
  } else if (type === 2) {
    return [year, month].join('-')
  } else if (type === 3) {
    return [year].join('')
  } else if (type === 4) {
    return [month , day , hour , minute, second].join('')
  } else if (type === 5) {
    return [year, month].join('-')
  } else if (type === 6) {
    return [year, month].join('-') + ' ' + [hour, minute].join(':')
  }else if (type === 7) {
    return [year, month , day , hour , minute, second].join('')
  }else if(type === 8){
    return [year, month, day].join('-') + ' ' + [hour, minute].join(':')
  }else if(type === 9){
    return [year-1, month].join('-')
  }
}

// 计算小时差 ----- yyyy-MM-dd HH:mm 使用版本
export function getBetweenHour_b(qssj, zzsj) {
  var rDate = {}
  var start = getDate_o(qssj)
  var end = getDate_o(zzsj)
  var Dtime = end - start
  rDate.days = Math.floor(Dtime / (24 * 3600 * 1000))
  var leave1 = Dtime % (24 * 3600 * 1000)
  rDate.hours = Math.floor(leave1 / (3600 * 1000))
  var leave2 = leave1 % (3600 * 1000)
  rDate.minutes = Math.floor(leave2 / (60 * 1000))

  return (rDate.days * 24 + rDate.hours + rDate.minutes / 60).toFixed(2)
}

//处理日期格式  yyyy-MM-dd HH:mm
export function getDate_o(strDate) {
  var a = strDate.split(' ')
  var b = a[0].split('-')
  var c = a[1].split(':')
  var date = new Date(b[0], b[1] - 1, b[2], c[0], c[1])
  return date
}
